package com.example.hao.animation_demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

public class View6Activity extends AppCompatActivity {

    private static final String TAG = "View6Activity";
    private View iv_star;

    int width;
    int height;
    private View iv_start;

    int w;
    int h;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view6);

        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();

        iv_star.post(new Runnable() {
            @Override
            public void run() {
                w = iv_star.getWidth();
                h = iv_star.getHeight();
                Log.d(TAG, "run: " + w + ";" + h);
            }
        });
    }

    private void initView() {
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        width = dm.widthPixels;//1080
        height = dm.heightPixels;//1902
        Log.d(TAG, "onCreate: " + width + "," + height);
        iv_star = findViewById(R.id.iv_star);
        iv_start = findViewById(R.id.iv_start);

        //
        anim = ValueAnimator.ofInt(width - 240, 0);
        Log.d(TAG, "initView: " + w);
        anim.setDuration(3000);
        anim.setRepeatCount(0);
        anim.setRepeatMode(ValueAnimator.RESTART);

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                iv_star.setX(value);
            }
        });

        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                iv_start.setVisibility(View.VISIBLE);
            }
        });
        //
        anim2 = ValueAnimator.ofInt(0, height - 240);
        anim2.setDuration(3000);
        anim2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value2 = (int) animation.getAnimatedValue();
                iv_star.setY(value2);
            }
        });
        anim2.start();

        //
        anim3 = ValueAnimator.ofFloat(1.0f, 0.0f);
        anim3.setDuration(3000);

        anim3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value3 = (float) animation.getAnimatedValue();
                iv_star.setScaleX(value3);
                iv_star.setScaleY(value3);
            }
        });
        anim3.start();
    }
    ValueAnimator anim;
    ValueAnimator anim2;
    ValueAnimator anim3;
    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
    public void run(View view) {
        anim.start();
        anim2.start();
        anim3.start();
//        iv_star.setVisibility(View.INVISIBLE);
    }
}
